﻿namespace fleetIt
{
    using System.Collections.Generic;
    using System.Web;
    using System.Windows;
    using System.Xml;
    using fleetIt.Core;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for LinkUI.xaml
    /// </summary>
    public partial class LinkUI : Window
    {
        private string _Url = string.Empty;
        public string Url
        {
            get
            {
                return this._Url;
            }
        }
        public LinkUI()
        {
            InitializeComponent();
        }

        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void LinkInsertCommand_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = this.txtURL.Text.Length >= 1;
        }

        private void LinkInsertCommand_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            string service = (this.cmbService.SelectedItem as ComboBoxItem).Tag.ToString();

            switch (service)
            {
                case "bit.ly":
                    this.InsertBitLy();
                    break;
                case "tiny.cc":
                    InsertTinyCC();
                    break;
                case "tinyurl.com":
                    this.InsertTinyUrl();
                    break;
                default:
                    break;
            }

            DialogResult = true;

        }
        private void InsertTinyCC()
        {
            using (Common common = new Common())
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("c", "rest_api");
                parameters.Add("m", "shorten");
                parameters.Add("login", "fleetit");
                parameters.Add("apiKey", "0acd1c3e-e4fa-422b-9463-9b15e17a6b7c");
                parameters.Add("version", "2.0.1");
                parameters.Add("longUrl", HttpUtility.UrlEncode(this.txtURL.Text));
                XmlDocument xmlDocument = common.GetResponseXml(
                    common.SendRequest(RequestMethod.GET, "http://tiny.cc/",
                    ResponseFormats.xml, parameters));

                string errorCode = xmlDocument.SelectSingleNode("/restapi/errorCode").InnerText;
                if (errorCode == "0")
                {
                    string url = xmlDocument.SelectSingleNode("//shorturl").InnerText;
                    this._Url = url;
                }
            }
        }
        private void InsertTinyUrl()
        {
            using (Common common = new Common())
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("url", HttpUtility.UrlEncode(this.txtURL.Text));
                string url = common.GetResponseString(common.SendRequest(RequestMethod.GET, "http://tinyurl.com/api-create.php",
                    ResponseFormats.none, parameters));
                this._Url = url;
            }
        }
        private void InsertBitLy()
        {
            using (Common common = new Common())
            {
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("login", "fleetit");
                parameters.Add("apiKey", "R_6ca2917356e8937157b2ed7b9c743447");
                parameters.Add("longUrl", HttpUtility.UrlEncode(this.txtURL.Text));
                XmlDocument xmlDocument = common.GetResponseXml(
                    common.SendRequest(RequestMethod.GET, "http://api.bit.ly/v3/shorten",
                    ResponseFormats.xml, parameters));
                string statusCode = xmlDocument.SelectSingleNode("/response/status_code").InnerText;
                if (statusCode == "200")
                {
                    string url = xmlDocument.SelectSingleNode("/response/data/url").InnerText;
                    this._Url = url;
                }
            }
        }
    }
}
